package com.yanqu.road.dao.impl.activity.suitang.data;

import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionJoinData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangUnionJoinDataDaoImpl extends TempDataObjectDao<CrossSuiTangUnionJoinData> {
    public static String getCrossSuiTangUnionJoinDataSql = "select * from `t_c_suitang_union_join_data` where `activityId`=? and `groupId`=?";
    public Map<String, CrossSuiTangUnionJoinData> getCrossSuiTangUnionJoinData(int activityId,int groupId) {
        Map<String, CrossSuiTangUnionJoinData> unionJoinDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangUnionJoinDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSuiTangUnionJoinData unionJoinData = parse(rs);
                unionJoinDataMap.put(unionJoinData.getUnionUid(), unionJoinData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return unionJoinDataMap;
    }

    public static String updateCrossSuiTangUnionJoinDataSql = "update `t_c_suitang_union_join_data` set unionBaseInfo=?,memberMap=? where activityId=? and groupId=? and unionUid=?";
    @Override
    protected boolean updateData(CrossSuiTangUnionJoinData crossSuiTangUnionJoinData) {
        crossSuiTangUnionJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionJoinData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionJoinData.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionJoinData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionJoinData.getUnionUid()));
        if (getDb().executeNoneQuery(updateCrossSuiTangUnionJoinDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangUnionJoinDataSql = "insert into `t_c_suitang_union_join_data`(activityId,groupId,unionUid,unionBaseInfo,memberMap) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangUnionJoinData crossSuiTangUnionJoinData) {
        crossSuiTangUnionJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangUnionJoinData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossSuiTangUnionJoinData.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionJoinData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangUnionJoinData.getMemberMap())));
        if (getDb().executeNoneQuery(insertCrossSuiTangUnionJoinDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangUnionJoinData parseData(ResultSet rs) throws SQLException {
        CrossSuiTangUnionJoinData crossSuiTangUnionJoinData = new CrossSuiTangUnionJoinData();
        crossSuiTangUnionJoinData.setActivityId(rs.getInt("activityId"));
        crossSuiTangUnionJoinData.setGroupId(rs.getInt("groupId"));
        crossSuiTangUnionJoinData.setUnionUid(rs.getString("unionUid"));
        crossSuiTangUnionJoinData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        crossSuiTangUnionJoinData.setMemberMap(JSON.parseObject(rs.getString("memberMap"), new TypeReference<ConcurrentHashMap<java.lang.Long, com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember>>() {}));

        return crossSuiTangUnionJoinData;
    }
}
